package com.gxa.jjty.cjr.service.impl;

import com.gxa.jjty.cjr.mapper.OrderoRecordMapper;
import com.gxa.jjty.cjr.mapper.ServerAndTorderVoMapper;
import com.gxa.jjty.cjr.mapper.TorderMapper;
import com.gxa.jjty.cjr.service.BillService;
import com.gxa.jjty.cjr.service.TorderService;
import com.gxa.jjty.cjr.utils.MessageUtil;
import com.gxa.jjty.core.common.ExceptionCode;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.ServerAndTorderVo;
import com.gxa.jjty.core.pojo.SystemMsg;
import com.gxa.jjty.core.pojo.Torder;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;
import com.gxa.jjty.core.util.JsonUtil;
import com.gxa.jjty.core.util.OrderIdUtil;
import com.gxa.jjty.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.OrderUtils;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author NITRO
 */
@Service("torderService")
public class TorderServiceImpl extends BaseServiceImpl<Torder> implements TorderService {


    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    private static final String TORDER_NUM_PRE="CJR";

    @Autowired
    private TorderMapper torderMapper;
    @Autowired
    private BillService billService;
    @Autowired
    private ServerAndTorderVoMapper serverAndTorderVoMapper;
    @Autowired
    private OrderoRecordMapper orderoRecordMapper;

    @Autowired
    MessageUtil messageUtil;

    @Override
    public void  addTorder1(Torder torder) {
        //设置订单生成时间
        torder.setOrderTime(new Date());
        torder.setOrderState("待付款");
        //订单编号生成
        /////////////////////////////////////////////
        //获得一个3位随机数
        StringBuilder code = new StringBuilder();
        Random ran = new Random();
        for (int i = 0; i < 3; i++) {
            code.append(Integer.valueOf(ran.nextInt(10)).toString());
        }
        //生成一个时间字符串:年月日时分秒毫秒
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String format = simpleDateFormat.format(timestamp);
        //前缀+时间+随机数生成订单编码
        torder.setOrderNumber(TORDER_NUM_PRE+format+code.toString());
        torder.setSerialNumber(OrderIdUtil.getOrderId());
        torderMapper.insert(torder);
        SystemMsg systemMsg = new SystemMsg();
        systemMsg.setReleaseTime(new Date());
        systemMsg.setMessageStatus(0);
        systemMsg.setTitle("你的订单已提交");
        systemMsg.setContent("你的订单已提交，请付款");
        messageUtil.putMsg(torder.getCjrId(),systemMsg);
    }

    @Override
    public void  addTorder2(Torder torder) {
        //设置订单生成时间
        torder.setOrderTime(new Date());
        torder.setOrderState("等待系统分配");
        torder.setPayTime(new Date());
        //订单编号生成
        /////////////////////////////////////////////
        //获得一个3位随机数
        StringBuilder code = new StringBuilder();
        Random ran = new Random();
        for (int i = 0; i < 3; i++) {
            code.append(Integer.valueOf(ran.nextInt(10)).toString());
        }
        //生成一个时间字符串:年月日时分秒毫秒
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String format = simpleDateFormat.format(timestamp);
        //前缀+时间+随机数生成订单编码
        torder.setOrderNumber(TORDER_NUM_PRE+format+code.toString());
        torder.setSerialNumber(OrderIdUtil.getOrderId());
        torderMapper.insert(torder);
        SystemMsg systemMsg = new SystemMsg();
        systemMsg.setReleaseTime(new Date());
        systemMsg.setMessageStatus(0);
        systemMsg.setTitle("你的订单已提交");
        systemMsg.setContent("你的订单已提交，完成付款");
        messageUtil.putMsg(torder.getCjrId(),systemMsg);

    }

    @Override
    public List<ServerAndTorderVo> getTorderBePaid(String number) {
        String state="待付款";
        Example example = new Example(ServerAndTorderVo.class);
        example.createCriteria().andEqualTo("orderNumber",number).andEqualTo("orderState",state);
        List<ServerAndTorderVo> serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
        return serverAndTorderVos;
    }

    @Override
    public void deleteTorder(String number,String cancelRemark) throws BusinessException {
        Example example = new Example(Torder.class);
        example.createCriteria().andEqualTo("orderNumber",number);
        List<Torder> torders = torderMapper.selectByExample(example);
        if (torders.size() < 0) {
            throw new BusinessException(ExceptionCode.TORDER_NULL);
        }
        Torder torder1 = torders.get(0);
        if (torder1.getOrderState().equals("待付款")) {
            Example example1 = new Example(Torder.class);
            example1.createCriteria().andEqualTo("orderNumber", number);
            List<Torder> torders1 = torderMapper.selectByExample(example1);
            for (Torder torder : torders1) {
                torder.setOrderState("已取消");
                torder.setCancelRemark(cancelRemark);
                torderMapper.updateByPrimaryKeySelective(torder);
                SystemMsg systemMsg = new SystemMsg();
                systemMsg.setReleaseTime(new Date());
                systemMsg.setMessageStatus(0);
                systemMsg.setTitle("你的订单已取消");
                systemMsg.setContent("你已取消订单");
                messageUtil.putMsg(torder.getCjrId(),systemMsg);
            }
        }else if (torder1.getOrderState().equals("等待系统分配") || torder1.getOrderState().equals("服务中") || torder1.getOrderState().equals("陪护员已接单")){
            Example example2 = new Example(Torder.class);
            example2.createCriteria().andEqualTo("orderNumber", number);
            List<Torder> torders2 = torderMapper.selectByExample(example2);
            for (Torder torder : torders2) {
                torder.setOrderState("退款中");
                torder.setCancelRemark(cancelRemark);
                torderMapper.updateByPrimaryKeySelective(torder);
                SystemMsg systemMsg = new SystemMsg();
                systemMsg.setReleaseTime(new Date());
                systemMsg.setMessageStatus(0);
                systemMsg.setTitle("你的订单申请退款");
                systemMsg.setContent("等待退款");
                messageUtil.putMsg(torder1.getCjrId(),systemMsg);
            }
            billService.paid(torder1.getOrderPrice(),torder1.getOrderPhone());

        }

    }

    @Override
    public void updateTorderFree(String number) {
        String state="待付款";
        Example example = new Example(Torder.class);
        example.createCriteria().andEqualTo("orderNumber", number).andEqualTo("orderState",state);
        List<Torder> torders = torderMapper.selectByExample(example);

        for (Torder torder : torders) {
            torder.setPayPrice(0.0);
            torder.setPayTime(new Date());
            torder.setPayType("免费");
            torder.setOrderState("等待系统分配");
            torder.setSerialNumber(String.valueOf(StringUtil.getNumber()));
            torderMapper.updateByPrimaryKeySelective(torder);
            SystemMsg systemMsg = new SystemMsg();
            systemMsg.setReleaseTime(new Date());
            systemMsg.setMessageStatus(0);
            systemMsg.setTitle("你的订单付款成功");
            systemMsg.setContent("请等待陪护员接单");
            messageUtil.putMsg(torder.getCjrId(),systemMsg);
        }

    }
    @Override
    public void updateTorder(String number) {
        String state="待付款";
        Example example = new Example(Torder.class);
        example.createCriteria().andEqualTo("orderNumber", number).andEqualTo("orderState",state);
        List<Torder> torders = torderMapper.selectByExample(example);
        for (Torder torder : torders) {
            torder.setPayPrice(torder.getOrderPrice());
            torder.setPayTime(new Date());
            torder.setOrderState("等待系统分配");
            torder.setSerialNumber(String.valueOf(StringUtil.getNumber()));
            torderMapper.updateByPrimaryKeySelective(torder);
            SystemMsg systemMsg = new SystemMsg();
            systemMsg.setReleaseTime(new Date());
            systemMsg.setMessageStatus(0);
            systemMsg.setTitle("你的订单付款成功");
            systemMsg.setContent("请等待陪护员接单");
            messageUtil.putMsg(torder.getCjrId(),systemMsg);
        }

    }

    @Override
    public List<ServerAndTorderVo> selectTorder(String phone,String number) {
        //redisTemplate.opsForList()

        String state="已完成";
        Example example = new Example(ServerAndTorderVo.class);
        example.createCriteria().andEqualTo("orderPhone",phone).andEqualTo("orderState",state).andEqualTo("orderNumber",number);
        List<ServerAndTorderVo> serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
        return serverAndTorderVos;
    }

    @Override
    public List<ServerAndTorderVo> getTorderList(String phone) {
        String state = "陪护员已接单";
        Example example = new Example(ServerAndTorderVo.class);
        example.orderBy("orderTime").desc();
        example.createCriteria().andEqualTo("orderPhone",phone).andEqualTo("orderState",state);
        List<ServerAndTorderVo> serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
        return serverAndTorderVos;
    }

    @Override
    public List<ServerAndTorderVo> getTorderListServiceIng(String phone) {
        String state = "服务中";
        Example example = new Example(ServerAndTorderVo.class);
        example.orderBy("orderTime").desc();
        example.createCriteria().andEqualTo("orderPhone",phone).andEqualTo("orderState",state);
        List<ServerAndTorderVo> serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
        return serverAndTorderVos;
    }

    @Override
    public void ToderRefund(String number,String cancelRemark) throws BusinessException {
        Example example = new Example(Torder.class);
        example.createCriteria().andEqualTo("orderNumber" ,number);
        List<Torder> torders = torderMapper.selectByExample(example);
        if (torders.size() < 0) {
            throw new BusinessException(ExceptionCode.TORDER_NULL);
        }
        Torder torder = torders.get(0);
        torder.setOrderState("退款中");
        torder.setCancelRemark(cancelRemark);
        billService.paid(torder.getOrderPrice(),torder.getOrderPhone());
        torderMapper.updateByPrimaryKeySelective(torder);
        SystemMsg systemMsg = new SystemMsg();
        systemMsg.setReleaseTime(new Date());
        systemMsg.setMessageStatus(0);
        systemMsg.setTitle("你的订单已申请退款");
        systemMsg.setContent("你的订单已申请退款");
        messageUtil.putMsg(torder.getCjrId(),systemMsg);
    }

    @Override
    public List<ServerAndTorderVo> selectAllTorderList(String phone) {
        Example example = new Example(ServerAndTorderVo.class);
        example.createCriteria().andEqualTo("orderPhone",phone);
        example.orderBy("orderTime").desc();
        List<ServerAndTorderVo> serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
        //List<Map> maps = torderMapper.selectAllAndImage(phone);
        return serverAndTorderVos;
    }

    @Override
    public List<ServerAndTorderVo> selectRefundFalse(String phone) {
        String state="退款失败";
        Example example = new Example(ServerAndTorderVo.class);
        example.orderBy("orderTime").desc();
        example.createCriteria().andEqualTo("orderPhone",phone).andEqualTo("orderState",state);
        List<ServerAndTorderVo> serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
        return serverAndTorderVos;
    }

    @Override
    public List<ServerAndTorderVo> selectRefundTrue(String phone) {
        String state="退款成功";
        Example example = new Example(ServerAndTorderVo.class);
        example.orderBy("orderTime").desc();
        example.createCriteria().andEqualTo("orderPhone",phone).andEqualTo("orderState",state);
        List<ServerAndTorderVo> serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
        return serverAndTorderVos;
    }

    @Override
    public List<ServerAndTorderVo> selectRefunding(String phone) {
        String state="退款中";
        Example example = new Example(ServerAndTorderVo.class);
        example.orderBy("orderTime").desc();
        example.createCriteria().andEqualTo("orderPhone",phone).andEqualTo("orderState",state);
        List<ServerAndTorderVo> serverAndTorderVos = serverAndTorderVoMapper.selectByExample(example);
        return serverAndTorderVos;
    }

    @Override
    public List<Torder> selectByNumbers(String number) {
        Example example = new Example(Torder.class);
        example.createCriteria().andEqualTo("orderNumber",number);
        List<Torder> torders = torderMapper.selectByExample(example);
        return torders;
    }

    @Override
    public List<ServerAndTorderVo> selectRefundFalseByOrderNumber(String orderNumber) {
        Example example = new Example(ServerAndTorderVo.class);
        example.createCriteria().andEqualTo("orderNumber",orderNumber);
        return serverAndTorderVoMapper.selectByExample(example);
    }

    @Override
    public List<ServerAndTorderVo> selectRefundTrueByOrderNumber(String orderNumber) {
        Example example = new Example(ServerAndTorderVo.class);
        example.createCriteria().andEqualTo("orderNumber",orderNumber);
        return serverAndTorderVoMapper.selectByExample(example);
    }

    @Override
    public List<ServerAndTorderVo> selectPayings(String orderNumber) {
        Example example = new Example(ServerAndTorderVo.class);
        example.createCriteria().andEqualTo("orderNumber",orderNumber);
        return serverAndTorderVoMapper.selectByExample(example);
    }

    @Override
    public List<ServerAndTorderVo> selectServering(String orderNumber) {
        Example example = new Example(ServerAndTorderVo.class);
        example.createCriteria().andEqualTo("orderNumber",orderNumber);
        return serverAndTorderVoMapper.selectByExample(example);
    }

    @Override
    public void sureTorderFinshed(Torder torder) {
        torderMapper.updateByPrimaryKeySelective(torder);
    }
}
